//
//  SMSExampleViewController.m
//  SMSExample
//

#import "SMSExampleViewController.h"

@implementation SMSExampleViewController

- (void)composeClick:(id)sender {
	if ([MFMessageComposeViewController canSendText]) {
		
		MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
		messageController.messageComposeDelegate = self;
		
		[messageController setBody:@"Hello world!"];
		
		[self presentModalViewController:messageController animated:YES];
		
	} else {
		
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
														message:@"Can't send SMS!"
													   delegate:self
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}

}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	UIButton *compose = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	CGRect composeFrame = CGRectMake(80,10,160,38);
	[compose setFrame:composeFrame];
	[compose setTitle:@"Compose SMS" forState:UIControlStateNormal];
	[compose addTarget:self
				action:@selector(composeClick:)
	  forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:compose];
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
				 didFinishWithResult:(MessageComposeResult)result {
	switch (result) {
		case MessageComposeResultCancelled:
			break;
			
		case MessageComposeResultSent:
			NSLog(@"SMS was sent");
			break;
			
		case MessageComposeResultFailed:
			NSLog(@"SMS failed to send");
			break;
	}
	[self dismissModalViewControllerAnimated:YES];
}

- (void)dealloc {
    [super dealloc];
}

@end
